home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / GSSTATE < prev    next >
Text File  |  1991-10-26  |  7KB  |  223 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gsstate.c */
  21. /* Miscellaneous graphics state operators for Ghostscript library */
  22. #include "gx.h"
  23. #include "memory_.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxmatrix.h"            /* for gzstate */
  27. #include "gzstate.h"
  28. #include "gzdevice.h"
  29. #include "gzcolor.h"            /* requires gxdevice.h */
  30. #include "gzht.h"
  31. #include "gzline.h"
  32. #include "gzpath.h"
  33.  
  34. /* Size of graphics state object (so clients can copy the top level) */
  35. const uint gs_state_sizeof = sizeof(gs_state);
  36.  
  37. /* Forward references */
  38. private int alloc_state_contents(P2(gs_state *, proc_alloc_t));
  39. private void free_state_contents(P1(gs_state *));
  40.  
  41. /* Allocate and initialize a graphics state. */
  42. private float
  43. null_transfer(floatp gray)
  44. {    return gray;
  45. }
  46. gs_state *
  47. gs_state_alloc(proc_alloc_t palloc, proc_free_t pfree)
  48. {    register gs_state *pgs = (gs_state *)(*palloc)(1, sizeof(gs_state), "gs_state_alloc");
  49.     if ( pgs == 0 ) return 0;
  50.     if ( alloc_state_contents(pgs, palloc) < 0 ) return 0;
  51.     pgs->saved = 0;
  52.     pgs->memory_procs.alloc = palloc;
  53.     pgs->memory_procs.free = pfree;
  54.     /* Initialize things not covered by initgraphics */
  55.     pgs->path->first_subpath = 0;
  56.     pgs->clip_path->first_subpath = 0;
  57.     pgs->halftone->width = pgs->halftone->height =
  58.         pgs->halftone->order_size = 0;
  59.     gs_sethalftonephase(pgs, 0, 0);
  60.     /* Initialize the color so that gx_remap_color won't crash. */
  61.     gx_set_gray_only(pgs->color, 0.0);
  62.     /* Initialize the transfer functions ditto. */
  63.     gx_set_transfer_only(pgs, null_transfer);
  64.     gs_nulldevice(pgs);
  65.     gs_setflat(pgs, 1.0);
  66.     /****** What about the font ? ******/
  67.     pgs->in_cachedevice = pgs->in_charpath = 0;
  68.     pgs->level = 0;
  69.     if ( gs_initgraphics(pgs) < 0 )
  70.        {    /* Something went very wrong */
  71.         return 0;
  72.        }
  73.     return pgs;
  74. }
  75.  
  76. /* Free a graphics state */
  77. int
  78. gs_state_free(gs_state *pgs)
  79. {    free_state_contents(pgs);
  80.     (*pgs->memory_procs.free)((char *)pgs, 1, sizeof(gs_state), "gs_state_free");
  81.     return 0;
  82. }
  83.  
  84. /* Save the graphics state */
  85. int
  86. gs_gsave(gs_state *pgs)
  87. {    gs_state *pnew = (gs_state *)(*pgs->memory_procs.alloc)(1, sizeof(gs_state), "gs_gsave");
  88.     if ( pnew == 0 )
  89.         return_error(gs_error_VMerror);
  90.     *pnew = *pgs;
  91.     if ( alloc_state_contents(pgs, pgs->memory_procs.alloc) < 0 )
  92.         return_error(gs_error_VMerror);
  93. #define gcopy(element,type)\
  94.     *pgs->element = *pnew->element
  95.     gcopy(path, gx_path);
  96.     gcopy(clip_path, gx_path);
  97.     gcopy(line_params, line_params);
  98.     gcopy(halftone, halftone);
  99.     gcopy(color, gs_color);
  100.     gcopy(dev_color, gx_device_color);
  101.     gcopy(device, device);
  102. #undef gcopy
  103.     gx_path_share(pgs->path);
  104.     gx_path_share(pgs->clip_path);
  105.     pgs->saved = pnew;
  106.     pgs->level++;
  107.     return 0;
  108. }
  109.  
  110. /* Restore the graphics state. */
  111. int
  112. gs_grestore(gs_state *pgs)
  113. {    gs_state *saved = pgs->saved;
  114.     if ( !saved ) return gs_gsave(pgs);    /* shouldn't happen */
  115.     free_state_contents(pgs);
  116.     *pgs = *saved;
  117.     (*pgs->memory_procs.free)((char *)saved, 1, sizeof(gs_state), "gs_grestore");
  118.     return (pgs->saved == 0 ? gs_gsave(pgs) : 0);
  119. }
  120.  
  121. /* Restore to the bottommost graphics state. */
  122. int
  123. gs_grestoreall(gs_state *pgs)
  124. {    if ( !pgs->saved ) return gs_gsave(pgs);    /* shouldn't happen */
  125.     while ( pgs->saved->saved ) gs_grestore(pgs);
  126.     return gs_grestore(pgs);
  127. }
  128.  
  129. /* Swap the saved pointer of the graphics state. */
  130. /* This is provided only for save/restore. */
  131. gs_state *
  132. gs_state_swap_saved(gs_state *pgs, gs_state *new_saved)
  133. {    gs_state *saved = pgs->saved;
  134.     pgs->saved = new_saved;
  135.     return saved;
  136. }
  137.  
  138. /* Swap the contents of two graphics states, except for the saved pointer. */
  139. /* This is provided only for save/restore. */
  140. void
  141. gs_state_swap(gs_state *p1, gs_state *p2)
  142. {    gs_state temp;
  143.     temp = *p1, *p1 = *p2, *p2 = temp;
  144.     /* Restore the saved pointers. */
  145.     p2->saved = p1->saved;
  146.     p1->saved = temp.saved;
  147. }
  148.  
  149. /* Reset most of the graphics state */
  150. int
  151. gs_initgraphics(register gs_state *pgs)
  152. {    int code;
  153.     gs_initmatrix(pgs);
  154.     if (    (code = gs_newpath(pgs)) < 0 ||
  155.         (code = gs_initclip(pgs)) < 0 ||
  156.         (code = gs_setlinewidth(pgs, 1.0)) < 0 ||
  157.         (code = gs_setlinecap(pgs, gs_cap_butt)) < 0 ||
  158.         (code = gs_setlinejoin(pgs, gs_join_miter)) < 0 ||
  159.         (code = gs_setdash(pgs, (float *)0, 0, 0.0)) < 0 ||
  160.         (code = gs_setgray(pgs, 0.0)) < 0 ||
  161.         (code = gs_setmiterlimit(pgs, 10.0)) < 0
  162.        ) return code;
  163.     return 0;
  164. }
  165.  
  166. /* setflat */
  167. int
  168. gs_setflat(gs_state *pgs, floatp flat)
  169. {    if ( flat <= 0.2 ) flat = 0.2;
  170.     else if ( flat > 100 ) flat = 100;
  171.     pgs->flatness = flat;
  172.     return 0;
  173. }
  174.  
  175. /* currentflat */
  176. float
  177. gs_currentflat(gs_state *pgs)
  178. {    return pgs->flatness;
  179. }
  180.  
  181. /* ------ Internal routines ------ */
  182.  
  183. /* Allocate the contents of a graphics state object. */
  184. /* Return -1 if the allocation fails. */
  185. private int
  186. alloc_state_contents(gs_state *pgs, proc_alloc_t palloc)
  187. {    char *elt;
  188.     static char *cname = "(gs)alloc_state_contents";
  189. #define galloc(element,type)\
  190.     if ( (elt = (*palloc)(1, sizeof(type), cname)) == 0 ) return -1;\
  191.     pgs->element = (type *)elt
  192.     galloc(path, gx_path);
  193.     galloc(clip_path, gx_path);
  194.     galloc(line_params, line_params);
  195.     galloc(halftone, halftone);
  196.     galloc(color, gs_color);
  197.     galloc(dev_color, gx_device_color);
  198.     galloc(device, device);
  199. #undef galloc
  200.     pgs->device_is_shared = 0;
  201.     return 0;
  202. }
  203.  
  204. /* Free the contents of a graphics state, but not the state itself. */
  205. private void
  206. free_state_contents(gs_state *pgs)
  207. {    proc_free_t pfree = pgs->memory_procs.free;
  208.     static char *cname = "(gs)free_state_contents";
  209.     gx_path_release(pgs->clip_path);
  210.     gx_path_release(pgs->path);
  211. #define gfree(element,type)\
  212.     (*pfree)((char *)pgs->element, 1, sizeof(type), cname)
  213.     if ( !pgs->device_is_shared )
  214.         gfree(device, device);
  215.     gfree(dev_color, gx_device_color);
  216.     gfree(color, gs_color);
  217.     gfree(halftone, halftone);
  218.     gfree(line_params, line_params);
  219.     gfree(clip_path, gx_path);
  220.     gfree(path, gx_path);
  221. #undef gfree
  222. }
  223.